﻿using System;
using Microsoft.SharePoint;
using System.Web;
using NCS.OCP.Common;
using NCS.OCP.Proxy;
using NCS.IConnect.ApplicationContexts;

namespace NCS.OCP.SharePointComponent.Layouts.NCS.OCP.Resource.Handlers
{
    public partial class CommitteeHandler : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            //get { throw new NotImplementedException(); }
            get {
                return false;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            int staffId;
            int redirectCtxId;
            string ctxAcronym;
            int currentCtxId;

           
            int.TryParse(context.Request.QueryString["staffId"].ToString(), out staffId);
            int.TryParse(context.Request.QueryString["ctxId"].ToString(), out redirectCtxId);
            int.TryParse(context.Request.QueryString["currentCtxId"].ToString(), out currentCtxId);
            ctxAcronym = context.Request.QueryString["ctxAcronym"].ToString();

          

            using (AccessFrameworkProxy proxy = new AccessFrameworkProxy())
            {
                bool isMember = false;
                isMember = proxy.IsContextMember(redirectCtxId, staffId);
                if (isMember)
                {
                    ApplicationContext.Current.SetKeyValue("ContextId", redirectCtxId);
                    ApplicationContext.Current.SetKeyValue("ContextAcronym", ctxAcronym);
                    context.Response.Redirect(string.Format(OCPConstant.SiteUrl.MyCommittee, redirectCtxId));
                  
                }
                else
                    context.Response.Redirect(string.Format(OCPConstant.SiteUrl.Public.MyCommittee, currentCtxId, redirectCtxId));
            }           
        }

        #endregion
    }
}
